# coding: utf-8
from datetime import datetime

from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import HttpResponseServerError
from django.shortcuts import render
from django.urls import reverse
from django.views import View
from leancloud import Object
from leancloud import Query
from leancloud.errors import LeanCloudError
import leancloud
import openpyxl
from . import Common
import time, datetime


#导入同义词配置表

class ImportSameNameExcel(View):
    def get(self, request):
        return render(request, 'import_data.html', {})
    def post(self, request):
        if request.FILES != None:
            excelFile = None
            for value in request.FILES:
                excelFile = value

            workSheet, workBook = Common.ReadExcel(request.FILES[excelFile])
            sameNameExcelDataList = []
            if workBook != None:
                for i in range(2, 10000):
                    itemName = workSheet.cell(row=i, column=1).value
                    if itemName == None:
                        break
                    sameNameList = []
                    for j in range(2, 10000):
                        sameName = workSheet.cell(row=i, column=j).value
                        if sameName == None:
                            break
                        sameNameList.append(sameName)
                    if len(sameNameList) > 0:
                        sameNameExcelDataList.append([itemName, sameNameList]);
            else:
                return render(request, 'import_data.html', {})
            if len(sameNameExcelDataList) > 0:
                #查询出数据库中分类配置
                sameNameInfo = leancloud.Object.extend('SameName')
                query = sameNameInfo.query
                query.limit(1000)
                SameName = query.find()
                #删除分类配置
                leancloud.Object.destroy_all(SameName)
                #新增分类配置
                allToDo = []
                Todo = leancloud.Object.extend('SameName')
                for elem in sameNameExcelDataList:
                    todo = Todo()
                    todo.set('ItemName', elem[0])
                    todo.set('SameNameList', elem[1])
                    allToDo.append(todo)
                Todo.save_all(allToDo)
        return render(request, 'import_data.html', {})

#导入分类表
class ImportSortExcel(View):
    def get(self, request):
        return render(request, 'import_data.html', {})
    def post(self, request):
        if request.FILES != None:
            excelFile = None
            for value in request.FILES:
                excelFile = value
            #存储上传的文件
            # file = leancloud.File("SortExcel.xlsx", request.FILES[excelFile])
            # file.save()

            workSheet, workBook = Common.ReadExcel(request.FILES[excelFile])
            sortExcelDataList = []
            if workBook != None:
                for i in range(2, 10000):
                    itemName = workSheet.cell(row=i, column=1).value
                    if itemName == None:
                        break
                    sortName = workSheet.cell(row=i, column=2).value
                    itemName = Common.SameNameConvert(itemName)
                    sortExcelDataList.append([itemName, sortName]);
            else:
                return render(request, 'import_data.html', {})
            if len(sortExcelDataList) > 0:
                #查询出数据库中分类配置
                itemSortInfo = leancloud.Object.extend('ItemSort')
                query = itemSortInfo.query
                query.limit(1000)
                itemSort = query.find()
                #删除分类配置
                leancloud.Object.destroy_all(itemSort)
                Todo = leancloud.Object.extend('ItemSort')
                #新增分类配置
                allToDo = []
                for elem in sortExcelDataList:
                    todo = Todo()
                    todo.set('ItemName', elem[0])
                    todo.set('SortName', elem[1])
                    allToDo.append(todo)
                Todo.save_all(allToDo)
        return render(request, 'import_data.html', {})

#导入单位配置表
class ImportUnitExcel(View):
    def get(self, request):
        return render(request, 'import_data.html', {})
    def post(self, request):
        if request.FILES != None:
            excelFile = None
            for value in request.FILES:
                excelFile = value

            workSheet, workBook = Common.ReadExcel(request.FILES[excelFile])
            unitExcelDataList = []
            if workBook != None:
                for i in range(2, 10000):
                    itemName = workSheet.cell(row=i, column=1).value
                    if itemName != None:
                        break
                    bigUnit = workSheet.cell(row=i, column=2).value
                    smallUnit = workSheet.cell(row=i, column=3).value
                    smallAmount = workSheet.cell(row=i, column=4).value
                    itemName = Common.SameNameConvert(itemName)
                    unitExcelDataList.append([itemName, bigUnit, smallUnit, smallAmount]);
            else:
                return render(request, 'import_data.html', {})
            if len(unitExcelDataList) > 0:
                #查询出数据库中分类配置
                itemUnitInfo = leancloud.Object.extend('ItemUnit')
                query = itemUnitInfo.query
                query.limit(1000)
                itemUnit = query.find()
                #删除分类配置
                leancloud.Object.destroy_all(itemUnit)
                #新增分类配置
                allToDo = []
                Todo = leancloud.Object.extend('ItemUnit')
                for elem in unitExcelDataList:
                    todo = Todo()
                    todo.set('ItemName', elem[0])
                    todo.set('BigUnit', elem[1])
                    todo.set('SmallUnit', elem[2])
                    todo.set('SmallAmount', elem[3])
                    allToDo.append(todo)
                Todo.save_all(allToDo)
        return render(request, 'import_data.html', {})

#导入销售成分表
class ImportSellMakeUpExcel(View):
    def get(self, request):
        return render(request, 'import_data.html', {})
    def post(self, request):
        if request.FILES != None:
            excelFile = None
            for value in request.FILES:
                excelFile = value

            workSheet, workBook = Common.ReadExcel(request.FILES[excelFile])
            makeUpExcelDataList = []
            if workBook != None:
                for i in range(2, 10000):
                    itemName = workSheet.cell(row=i, column=1).value
                    if itemName == None:
                        break
                    makeUpList = []
                    for j in range(2, 10000):
                        if j % 3 == 2:
                            makeUpElemList = []
                            makeUpName = workSheet.cell(row=i, column=j).value
                            if makeUpName == None:
                                break
                            makeUpSameName = Common.SameNameConvert(makeUpName)
                            useValue = workSheet.cell(row=i, column=(j+1)).value
                            useUnit = workSheet.cell(row=i, column=(j+2)).value
                            makeUpElemList = [makeUpSameName, useValue, useUnit]
                            makeUpList.append(makeUpElemList)
                    itemName = Common.SameNameConvert(itemName)
                    makeUpExcelDataList.append([itemName, makeUpList])
            else:
                return render(request, 'import_data.html', {})
            if len(makeUpExcelDataList) > 0:
                #查询出数据库中分类配置
                sellMakeUpInfo = leancloud.Object.extend('SellMakeUp')
                query = sellMakeUpInfo.query
                query.limit(1000)
                sellMakeUp = query.find()
                #删除分类配置
                leancloud.Object.destroy_all(sellMakeUp)
                #新增分类配置
                allToDo = []
                Todo = leancloud.Object.extend('SellMakeUp')
                for elem in makeUpExcelDataList:
                    todo = Todo()
                    todo.set('ItemName', elem[0])
                    todo.set('MakeUpList', elem[1])
                    allToDo.append(todo)
                Todo.save_all(allToDo)
        return render(request, 'import_data.html', {})

#导入采购表
class ImportBuyExcel(View):
    def get(self, request):
        return render(request, 'import_data.html', {})
    def post(self, request):
        if request.FILES != None:
            excelFile = None
            for value in request.FILES:
                excelFile = value
            workSheet, workBook = Common.ReadExcel(request.FILES[excelFile])
            buyExcelDataList = []
            if workBook != None:
                overFlag = False
                for i in range(2, 10000):
                    dataList = []
                    for j in range(1, 13):
                        value = workSheet.cell(row=i, column=j).value
                        dataList.append(value)
                        if j == 1 and value == None:
                            overFlag = True
                    if overFlag:
                        break
                    buyExcelDataList.append(dataList)
                if len(buyExcelDataList) > 0:
                    #新增采购记录
                    allToDo = []
                    Todo = leancloud.Object.extend('BuyRecord')
                    curSeller = ""
                    curSellerLabel = ""
                    for elem in buyExcelDataList:
                        if elem[1] != None and curSellerLabel != elem[1]:
                            curSellerLabel = elem[1]
                            curSeller = elem[2]
                        timeArray = time.strptime(str(elem[0]), "%Y-%m-%d %H:%M:%S")
                        elem[4] = Common.SameNameConvert(elem[4])
                        todo = Todo()
                        todo.set('Year', timeArray[0])
                        todo.set('Month', timeArray[1])
                        todo.set('Day', timeArray[2])
                        todo.set('ListNum', elem[1])
                        todo.set('Seller', curSeller)
                        todo.set('Sort', elem[3])
                        todo.set('ItemName', elem[4])
                        todo.set('Unit', elem[5])
                        todo.set('Amount', elem[6])
                        todo.set('Spec', elem[7])
                        todo.set('Price', elem[8])
                        todo.set('TotalPrice', elem[9])
                        todo.set('IsCal', False)
                        allToDo.append(todo)
                    Todo.save_all(allToDo)
        return render(request, 'import_data.html', {})

#导入销售表
class ImportSellExcel(View):
    def get(self, request):
        return render(request, 'import_data.html', {})
    def post(self, request):
        if request.FILES != None:
            excelFile = None
            for value in request.FILES:
                excelFile = value
            workSheet, workBook = Common.ReadExcel(request.FILES[excelFile])
            sellExcelDataList = []
            if workBook != None:
                #店铺名称
                shopName = workSheet.cell(row=4, column=10).value
                for i in range(7, 10000):
                    checkData = workSheet.cell(row=i, column=1).value
                    if checkData == None:
                        break
                    itemName = workSheet.cell(row=i, column=2).value
                    if itemName == None:
                        continue
                    unit = workSheet.cell(row=i, column=3).value
                    basicPrice = workSheet.cell(row=i, column=5).value
                    sellAmount = workSheet.cell(row=i, column=7).value
                    itemName = Common.SameNameConvert(itemName)
                    sellExcelDataList.append([itemName, unit, basicPrice, sellAmount])
                if len(sellExcelDataList) > 0:
                    #时间需要前端传入，todo
                    timeStr = "2019-11-11 00:00:00"
                    timeArray = time.strptime(timeStr, "%Y-%m-%d %H:%M:%S")
                    allToDo = []
                    Todo = leancloud.Object.extend('SellRecord')
                    for elem in sellExcelDataList:
                        todo = Todo()
                        todo.set('Year', timeArray[0])
                        todo.set('Month', timeArray[1])
                        todo.set('Day', timeArray[2])
                        todo.set('Shop', shopName)
                        todo.set('ItemName', elem[0])
                        todo.set('Unit', elem[1])
                        todo.set('BasicPrice', elem[2])
                        todo.set('SellAmount', elem[3])
                        todo.set('IsCal', False)
                        allToDo.append(todo)
                    Todo.save_all(allToDo)
        return render(request, 'import_data.html', {})

#导入盘点表
class ImportInventoryExcel(View):
    def get(self, request):
        return render(request, 'import_data.html', {})
    def post(self, request):
        if request.FILES != None:
            excelFile = None
            for value in request.FILES:
                excelFile = value
            workSheet, workBook = Common.ReadExcel(request.FILES[excelFile])
            inventoryExcelDataList = []
            if workBook != None:
                for i in range(2, 10000):
                    itemName = workSheet.cell(row=i, column=2).value
                    if itemName == None:
                        break
                    timeRecord = workSheet.cell(row=i, column=1).value
                    itemAmount = workSheet.cell(row=i, column=3).value
                    itemUnit = workSheet.cell(row=i, column=4).value
                    inventoryExcelDataList.append([str(timeRecord), itemName, itemAmount, itemUnit])
                if len(inventoryExcelDataList) > 0:
                    allToDo = []
                    Todo = leancloud.Object.extend('Inventory')
                    for elem in inventoryExcelDataList:
                        timeArray = time.strptime(elem[0], "%Y-%m-%d %H:%M:%S")
                        todo = Todo()
                        todo.set('Year', timeArray[0])
                        todo.set('Month', timeArray[1])
                        todo.set('Day', timeArray[2])
                        todo.set('ItemName', elem[1])
                        todo.set('Unit', elem[3])
                        todo.set('Amount', elem[2])
                        allToDo.append(todo)
                    Todo.save_all(allToDo)
        return render(request, 'import_data.html', {})



